﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.Configuration;

namespace IncomingChatObserver
{
    public delegate void ChatEventHandler(object sender, ChatEventArgs e);
    public class ChatEventArgs : EventArgs
    {
        private string _guid;

        public string Guid
        {
            get { return _guid; }
            set { _guid = value; }
        }
        private string _password;

        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }

        public ChatEventArgs() { }

        public ChatEventArgs(string guid, string password)
        {
            this._guid = guid;
            this._password = password;
        }
    }

    public class ChatObserver : IDisposable
    {
        private ServiceHost _hService;

        public event ChatEventHandler ChatObserved;

        protected void OnChatObserved(string guid, string password)
        {
            ChatEventHandler handler = ChatObserved;
            if (handler != null)
                handler(this, new ChatEventArgs(guid, password));
        }

        public void Start()
        {
            string port = ConfigurationSettings.AppSettings["port"];
            IncomingChatService.Instance.NotifyChat = this.OnChatObserved;
            Uri _baseAddress = new Uri("net.tcp://localhost:" + port + "/P2P");
            Uri _mexAddress = new Uri("http://localhost:9003/sMessengerClient");
            string _address = "client";

            var tcpTransportBindingElement = new TcpTransportBindingElement();
            CustomBinding _binding = new CustomBinding(tcpTransportBindingElement);

            _hService = new ServiceHost(IncomingChatService.Instance, _baseAddress);

            ServiceMetadataBehavior metadataBehavior = _hService.Description.Behaviors.Find<ServiceMetadataBehavior>();
            if (metadataBehavior == null)
            {
                metadataBehavior = new ServiceMetadataBehavior();
                metadataBehavior.HttpGetEnabled = true;
                metadataBehavior.HttpGetUrl = _mexAddress;
                _hService.Description.Behaviors.Add(metadataBehavior);
            }
            else
            {
                metadataBehavior.HttpGetEnabled = true;
                metadataBehavior.HttpGetUrl = _mexAddress;
            }
            _hService.AddServiceEndpoint(typeof(IncomingChatService), _binding, _address);

            _hService.BeginOpen(OpenEnded, null);
        }

        private void OpenEnded(IAsyncResult result)
        {
            _hService.EndOpen(result);
        }

        #region IDisposable Members

        public void Dispose()
        {
            if (_hService != null)
                _hService.Close();
        }

        #endregion
    }

}
